package hos.http;

import android.util.Log;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: HttpLogUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-04-03 22:29
 */
public class HttpLogUtils {

    private static final int MAX_LEN = 1100;// fit for Chinese character

    private static boolean isDebug() {
        return HttpConfig.config().isDebug();
    }

    public static void printStackTrace(Throwable e) {
        if (e != null && isDebug()) {
            Log.e("Http", "throw:", e);
        }
    }

    private static void printD(String info) {
        Log.d("Http", info);
    }

    public static void print(Object info) {
        String printInfo = null;
        if (info instanceof List) {
            printInfo = JsonUtils.toJson((List<?>) info);
        } else if (info instanceof Map) {
            printInfo = JsonUtils.toJson((Map) info);
        } else if (info instanceof File) {
            printInfo = ((File) info).getAbsolutePath();
        } else {
            if (info != null) {
                printInfo = info.toString();
            }
        }
        if (printInfo != null && isDebug()) {
            int len = printInfo.length();
            int countOfSub = len / MAX_LEN;
            if (countOfSub > 0) {
                int index = 0;
                for (int i = 0; i < countOfSub; i++) {
                    printD(printInfo.substring(index, index + MAX_LEN));
                    index += MAX_LEN;
                }
                if (index != len) {
                    printD(printInfo.substring(index, len));
                }
            } else {
                printD(printInfo);
            }
        }
    }

    public static void requestLog(HttpRequest<?> request, String type) {
        if (isDebug()) {
            print("------------------------------------" + type + "Start-------------------------------");
            String url = request.getUrl();
            String method = request.getMethod();
            print("Url:" + url);
            print("Method:" + method);
            print("ContentType:" + request.mediaType);
            Map<String, String> headerMap = request.getHeader();
            if (!headerMap.isEmpty()) {
                String header = HttpUtils.urlParam(headerMap);
                String headerJson = JsonUtils.toJson(headerMap);
                print("Header:"+header);
                print("HeaderJson:"+headerJson);
            }
            if (request instanceof HttpBaseRequestSoap) {
                HttpBaseRequestSoap<?> requestBody = (HttpBaseRequestSoap<?>) request;
                String dataXml = requestBody.getDataXml();
                if (dataXml != null) {
                    print(dataXml);
                }
            }
            // 处理请求地址
            List<HttpParam> queryParameters = request.getQueryParameters();
            if (!queryParameters.isEmpty()) {
                String urlParam = HttpUtils.urlParam(queryParameters);
                String urlJson = JsonUtils.toJson(queryParameters);
                print("UrlParam:"+urlParam);
                print("UrlJson:"+urlJson);
            }
            List<HttpParam> bodyParameters = request.getBodyParameters();
            if (!bodyParameters.isEmpty()) {
                String bodyParam = HttpUtils.urlParam(bodyParameters);
                String bodyJson = JsonUtils.toJson(queryParameters);
                print("BodyParam:"+bodyParam);
                print("BodyJson:"+bodyJson);
            }
            if (request instanceof HttpBaseRequestBody) {
                HttpBaseRequestBody<?> requestBody = (HttpBaseRequestBody<?>) request;
                Object data = requestBody.getData();
                if (data != null) {
                    try {
                        print(String.valueOf(data));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                List<HttpFile> fileList = requestBody.getFileList();
                String fileJson = JsonUtils.toFileJson(fileList);
                if (fileJson != null) {
                    try {
                        print(fileJson);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            if (request instanceof HttpBaseRequestDownload) {
                HttpBaseRequestDownload<?> requestBody = (HttpBaseRequestDownload<?>) request;
                String json = requestBody.toParamJson();
                if (json != null) {
                    print(json);
                }
            }
            print("------------------------------------" + type + "End-------------------------------");
        }
    }

    public static void responseLog(HttpRequest<?> request, Object result) {
        requestLog(request, "Response");
        if (isDebug()) {
            print("------------------------------------ResponseResultStart-------------------------------");
            print(result);
            print("------------------------------------ResponseResultEnd-------------------------------");
        }
    }
}
